/***********************************************************
 *  File_Name  : XJList.cpp
 *  File_Path   :
 *  File_Encode : UTF8
 *  Description :
 *
 *  OS : Linux, UNIX
 *  LastVersion  : 20150128
 *
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2015-1-28
 *  Version     : 20150128
 *  Description :
 *
 *  Edit Author :
 *  Edit Date   :
 *  Version     :
 *  Description :
 *
 ***********************************************************/
#include "XJList.h"
#include <stdlib.h>
 #include <string.h>
namespace XJUtilClass {

XJList::XJList() {
	// TODO Auto-generated constructor stub
	this->nCount = 0;
	this->pHead = 0;
	this->pTail = 0;
}

XJList::~XJList() {
	// TODO Auto-generated destructor stub
	Destroy();
}

int XJList::Size(){
	return this->nCount;
}

void *XJList::Front(){
	void *pData = 0;
	if (this){
		if(this->pHead){
			pData =  this->pHead->pData;
		}
	}
	return pData;
}

void *XJList::Back(){
	void *pData = 0;
	if (this){
		if(this->pTail){
			pData =  this->pTail->pData;
		}
	}
	return pData;
}

void XJList::PopFront(){
	if (this){
		if (this->pHead){
			XJListNode *pNode = this->pHead;
			this->pHead = pNode->pNext;
			if (this->pHead)
				this->pHead->pPrev = 0;
			else
				this->pTail = 0;
			this->nCount--;
			free(pNode);
			pNode = 0;
		}
	}
}

void XJList::PopBack(){
	if (this){
		if (this->pTail){
			XJListNode *pNode = this->pTail;
			this->pTail = pNode->pPrev;
			if (this->pTail)
				this->pTail->pNext = 0;
			else
				this->pHead = 0;
			this->nCount--;
			free(pNode);
			pNode = 0;
		}
	}
}

void XJList::Clear(){
	if (this){
		XJListNode *pNode = this->pHead;
		while(pNode){
			this->pHead = pNode->pNext;
			free(pNode);
			pNode = this->pHead;
			this->nCount--;
		}
		this->pHead = 0;
		this->pTail = 0;
		this->nCount = 0;
	}
}

void XJList::PushFront(void *pData){
	if (this == 0)
		return ;
	XJListNode *pNode = (XJListNode *)malloc(sizeof(XJListNode));
	memset(pNode, 0, sizeof(XJListNode));
	pNode->pData = pData;
	pNode->pList = this;
	if (this->pHead == 0){
		this->pHead = pNode;
		this->pTail = pNode;
	}else{
		this->pHead->pPrev =  pNode;
		pNode->pNext = this->pHead;
		this->pHead = pNode;
	}
	this->nCount++;
}
void XJList::PushBack(void *pData){
	if (this == 0)
		return ;
	XJListNode *pNode = (XJListNode *)malloc(sizeof(XJListNode));
	memset(pNode, 0, sizeof(XJListNode));
	pNode->pData = pData;
	pNode->pList = this;
	if (this->pHead == 0){
		this->pHead = pNode;
		this->pTail = pNode;
	}else{
		this->pTail->pNext =  pNode;
		pNode->pPrev = this->pTail;
		this->pTail = pNode;
	}
	this->nCount++;
}
void XJList::Destroy(){
	if (this){
		XJListNode *pNode = this->pHead;
		while(pNode){
			this->pHead = pNode->pNext;
			free(pNode);
			pNode = this->pHead;
			this->nCount--;
		}
		this->pHead = 0;
		this->pTail = 0;
		this->nCount = 0;
		//free(this);
	}
}

XJListNode* XJList::NodeBegin(){
	XJListNode* pNode = 0;
	if(this){
		pNode = this->pHead;
	}
	return pNode;
}

XJListNode* XJList::NodeEnd(){
	XJListNode* pNode = 0;
	if(this){
		pNode = this->pTail;
	}
	return pNode;
}

XJListNode* XJList::NodePrev(XJListNode *pListNode){
	XJListNode* pNode = 0;
	if(pListNode){
		pNode = pListNode->pPrev;
	}
	return pNode;
}

XJListNode* XJList::NodeNext(XJListNode *pListNode){
	XJListNode* pNode = 0;
	if(pListNode){
		pNode = pListNode->pNext;
	}
	return pNode;
}

void* XJList::NodeData(XJListNode *pListNode){
	void* pData = 0;
	if(pListNode){
		pData = pListNode->pData;
	}
	return pData;
}

XJListNode* XJList::NodeErase(XJListNode *pListNode){
	XJListNode *pNextNode = 0;
	if(pListNode){
		if (pListNode->pPrev){
			pListNode->pPrev->pNext = pListNode->pNext;
		}else{
			((XJList *)(pListNode->pList))->pHead = pListNode->pNext;
		}
		if (pListNode->pNext){
			pListNode->pNext->pPrev = pListNode->pPrev;
			pNextNode = pListNode->pNext;
		}else{
			((XJList *)(pListNode->pList))->pTail = pListNode->pPrev;
		}
		((XJList *)(pListNode->pList))->nCount--;
		free(pListNode);
	}
	return pNextNode;
}

XJListNode* XJList::NodeAfterInsert(XJListNode *pListNode, void *pData){
	if (pListNode == 0){
		return 0;
	}
	XJListNode *pNode = (XJListNode *)malloc(sizeof(XJListNode));
	memset(pNode, 0, sizeof(XJListNode));
	pNode->pData = pData;
	pNode->pList = pListNode->pList;
	pNode->pPrev = pListNode;
	pNode->pNext = pListNode->pNext;
	if (pListNode->pNext){
		pListNode->pNext->pPrev = pNode;
	}else{
		((XJList *)(pListNode->pList))->pTail = pNode;
	}
	pListNode->pNext = pNode;
	((XJList *)(pListNode->pList))->nCount++;
	return pNode;
}

XJListNode* XJList::NodeBeforeInsert(XJListNode *pListNode, void *pData){
	if (pListNode == 0){
		return 0;
	}
	XJListNode *pNode = (XJListNode *)malloc(sizeof(XJListNode));
	memset(pNode, 0, sizeof(XJListNode));
	pNode->pData = pData;
	pNode->pList = pListNode->pList;
	pNode->pPrev = pListNode->pPrev;
	pNode->pNext = pListNode;
	if (pListNode->pPrev){
		pListNode->pPrev->pNext = pNode;
	}else{
		((XJList *)(pListNode->pList))->pHead = pNode;
	}
	pListNode->pPrev = pNode;
	((XJList *)(pListNode->pList))->nCount++;
	return pNode;
}

} /* namespace XJUtilClass */
